package controller

import (
	models "fileshare/controller/modles"
	"fileshare/controller/server"
	"fileshare/controller/tools"
	"fileshare/utils"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

/**
* @program: fileshare
*
* @description:
*
* @author: Koco.Allien
*
* @create: 2020-04-06 11:56
**/
//		<a href="/sf?shareid=1&&active={{.active}}">自己分享给别人</a>
//        <a href="/sf?shareid=2&&active={{.active}}">别人分享给自己</a>
//        <a href="/sf?shareid=3&&active={{.active}}">自己分享给大家</a>
func ShareSelfController(c *gin.Context) {
	aid := tools.GatQuery(c, "active", 3)

	shareid := tools.GatQuery(c, "shareid", 1)

	utils.MsgToLog("个人分享:ShareSelfController", aid, shareid)

	data, u, ok := tools.StartDate(c, "个人分享", aid)

	if !ok || shareid < 1 || shareid > 3 {
		data["error"] = "操作超出权限或参数错误"
		c.HTML(http.StatusOK, "index.html", data)
	} else {
		var files []models.BelongInfo
		var count int
		var msg string

		switch shareid {
		case 2:
			files, count = server.GetToMeFiles(-1, 100, u.ID)
			msg = "别人分享给自己"
		case 3:
			files, count = server.GetToAllPeopleFiles(-1, 100, u.ID)
			msg = "自己分享给大家"
		default:
			files, count = server.GetToOtherFiles(-1, 100, u.ID)
			msg = "自己分享给别人"
		}
		data["shareid"] = shareid
		data["files"] = files
		data["count"] = count
		data["msg"] = msg
		c.HTML(http.StatusOK, "sharefile.html", data)
	}
}

//删除分享 fid={{$elem.ID}}&&active={{.active}}&&shareid=1
func SfdeleteController(c *gin.Context) {
	fid := tools.GatQuery(c, "fid", 1)
	aid := tools.GatQuery(c, "fid", 3)

	shareid := tools.GatQuery(c, "shareid", 1)

	utils.MsgToLog("个人分享:SfdeleteController", aid, shareid)

	data, u, ok := tools.StartDate(c, "个人分享", aid)

	if !ok || shareid < 1 || shareid > 3 || fid == 1 {
		data["error"] = "操作超出权限或参数错误"
		c.HTML(http.StatusOK, "index.html", data)
	} else {
		server.DelFile(fid)
		data["error"] = "删除成功"

		var files []models.BelongInfo
		var count int
		var msg string

		switch shareid {
		case 2:
			files, count = server.GetToMeFiles(-1, 100, u.ID)
			msg = "别人分享给自己"
		case 3:
			files, count = server.GetToAllPeopleFiles(-1, 100, u.ID)
			msg = "自己分享给大家"
		default:
			files, count = server.GetToOtherFiles(-1, 100, u.ID)
			msg = "自己分享给别人"
		}
		data["shareid"] = shareid
		data["files"] = files
		data["count"] = count
		data["msg"] = msg
		c.HTML(http.StatusOK, "sharefile.html", data)
	}
}

// 添加分享 用户--> 文件
func SfaddufController(c *gin.Context) {
	aid := tools.GatQuery(c, "active", 2)
	//获取uid
	uid := tools.GatQuery(c, "uid", 1)

	data, u, ok := tools.StartDate(c, "分享文件", aid)

	utils.MsgToLog("个人分享:SfaddufController  添加分享 用户--> 文件 ", uid, u)

	if !ok || uid < 1 {
		data["error"] = "操作超出权限或参数错误"
		c.HTML(http.StatusOK, "index.html", data)
		return
	}

	touser := server.GetUser(uid)
	data["uname"] = touser.Name
	data["uid"] = touser.ID
	files, count := server.GetSelfFiles(-1, 100, u.ID)
	data["files"] = files
	data["count"] = count
	c.HTML(http.StatusOK, "shareuf.html", data)
}

//添加分享
func SfaddufControllerPost(c *gin.Context) {
	aid := tools.GatQuery(c, "active", 2)
	//获取uid
	uid := tools.GatQuery(c, "uid", 1)

	data, u, ok := tools.StartDate(c, "分享文件", aid)

	utils.MsgToLog("个人分享:SfaddufController  添加分享 用户--> 文件 ", uid, u)

	if !ok || uid < 1 {
		data["error"] = "操作超出权限或参数错误"
		c.HTML(http.StatusOK, "index.html", data)
		return
	} else {
		//获取参数信息
		fids := c.PostFormArray("fids")
		utils.MsgToLog("个人分享:SfaddfuControllerPost  files ", fids)
		//展示文件  分享
		for _, id := range fids {
			//插入分享数据信息
			fid, _ := strconv.Atoi(id)
			f := server.GetFile(fid)
			server.AddSharef(uid, 0, f)
		}
	}
	data["error"] = "成功"

	touser := server.GetUser(uid)
	data["uname"] = touser.Name
	data["uid"] = touser.ID
	files, count := server.GetSelfFiles(-1, 100, u.ID)
	data["files"] = files
	data["count"] = count
	c.HTML(http.StatusOK, "shareuf.html", data)
}

// 添加分享 文件--> 用户 sfaddfu? fid=18 && active=2
func SfaddfuController(c *gin.Context) {
	aid := tools.GatQuery(c, "active", 2)
	//获取fid
	fid := tools.GatQuery(c, "fid", 1)

	data, u, ok := tools.StartDate(c, "分享文件", aid)

	utils.MsgToLog("个人分享:SfaddfuController  添加分享 文件--> 用户 ", fid, u)

	if !ok || fid < 1 {
		data["error"] = "操作超出权限或参数错误"
		c.HTML(http.StatusOK, "index.html", data)
		return
	} else {

		//展示用户列表  分享
		f := server.GetFile(fid)
		//分享链接的形式
		url := tools.GatQuery(c, "url", 0)
		if f.UID != u.ID {
			data["error"] = "操作超出权限或参数错误"
			//data["files"] = server.GetPublicFiles()
			c.HTML(http.StatusOK, "index.html", data)
			return
		}
		var msg string

		defer func() {
			data["fname"] = f.FFname
			data["fid"] = f.ID
			//去除 包含该文件的用户
			users, count := server.GetUsers(-1, 100)
			data["users"] = users
			data["count"] = count
			data["error"] = msg
			c.HTML(http.StatusOK, "sharefu.html", data)
		}()

		if url == 1 {
			//添加分享链接
			msg = server.AddShareUrl(f)
			return
		}

	}

}

//添加分享
func SfaddfuControllerPost(c *gin.Context) {
	aid := tools.GatQuery(c, "active", 2)
	//获取fid
	fid := tools.GatQuery(c, "fid", 1)

	data, u, ok := tools.StartDate(c, "分享文件", aid)

	utils.MsgToLog("个人分享:SfaddfuControllerPost  添加分享 文件--> 用户 ", fid, u)

	if !ok || fid < 1 {
		data["error"] = "操作超出权限或参数错误"
		c.HTML(http.StatusOK, "index.html", data)
		return
	} else {
		//获取参数信息
		//puid := c.PostFormArray("puid")
		uids := c.PostFormArray("uids")
		utils.MsgToLog("个人分享:SfaddfuControllerPost  用户s ", uids)
		//展示用户列表  分享
		f := server.GetFile(fid)
		if f.UID != u.ID || len(uids) == 0 {
			data["error"] = "操作超出权限或参数错误"
			//data["files"] = server.GetPublicFiles()
			c.HTML(http.StatusOK, "index.html", data)
			return
		} else {
			//查看是否是分享给所有人
			var toall bool
			for _, id := range uids {
				if id == "1" {
					toall = true
					break
				}
				//插入分享数据信息
			}
			if toall {
				server.AddSharef(1, 0, f)
			} else {
				//添加分享的文件
				for _, id := range uids {
					//插入分享数据信息
					toid, _ := strconv.Atoi(id)
					server.AddSharef(toid, 0, f)
				}
			}
			data["error"] = "成功"
		}

		data["fname"] = f.FFname
		data["fid"] = f.ID
		users, count := server.GetUsers(-1, 100)
		data["users"] = users
		data["count"] = count
		c.HTML(http.StatusOK, "sharefu.html", data)
	}
}
